//
//  HQForgetPasswordViewController.m
//  MobileiOS
//
//  Created by 郝庆松 on 2018/5/11.
//  Copyright © 2018年 PCITC. All rights reserved.
//

#import "HQForgetPasswordViewController.h"

@interface HQForgetPasswordViewController ()

@end

@implementation HQForgetPasswordViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self setPlaceHolderColor:_phoneNo];
    [self setPlaceHolderColor:_oldPassword];
    [self setPlaceHolderColor:_newPassword];
    [self setPlaceHolderColor:_contentPassword];
    [self addKeyBoardObserver];
}

/**
 发送验证码
 
 @param sender 按钮
 */
- (IBAction)sendAuthCodeItem:(id)sender {
    if([self isInputEmpty:_phoneNo.text]){
        [self tostView:@"请输入手机号"];
        return;
    }
    if(![HQTool phoneNoRegex:_phoneNo.text]){
        [self tostView:@"请输入正确的手机号"];
        return;
    }
    [self showHudText:@""];
    [HQNetWork requetParam:[HQParam userGetKey:_phoneNo.text] withMethod:[HQMethod userGetKey] suc:^(NSDictionary *sucData) {
        [self hiddnHud];
        if([sucData[kStatus] integerValue] == 1){
            [self tostView:@"验证码发送成功，请注意查收!"];
            [[[HQTool alloc]init] startVerify:_getAuthCode];
        }else{
            [self tostView:sucData[kInfo]];
        }
    } fail:^{
        [self hiddenErrorHud];
    }];
}

//当键盘出现或改变时调用
- (void)keyboardWillShow:(NSNotification *)aNotification{
    NSDictionary *userInfo = [aNotification userInfo];
    NSValue *aValue = [userInfo objectForKey:UIKeyboardFrameEndUserInfoKey];
    CGRect keyboardRect = [aValue CGRectValue];
    _viewHeightConstraints.constant = 550 + keyboardRect.size.height;
}

//当键退出时调用
- (void)keyboardWillHide:(NSNotification *)aNotification{
    _viewHeightConstraints.constant = 550;
}

/**
 确认修改
 
 @param sender 确认修改
 */
- (IBAction)contentEditItem:(id)sender {
    if([self isInputEmpty:_phoneNo.text]){
        [self tostView:@"请输入手机号"];
        return;
    }
    if(![HQTool phoneNoRegex:_phoneNo.text]){
        [self tostView:@"请输入正确的手机号"];
        return;
    }
    if([self isInputEmpty:_oldPassword.text]){
        [self tostView:@"请输入验证码"];
        return;
    }
    if([self isInputEmpty:_newPassword.text]){
        [self tostView:@"请输入新密码"];
        return;
    }
    if([self isInputEmpty:_contentPassword.text]){
        [self tostView:@"请再次输入新密码"];
        return;
    }
    if(![_newPassword.text isEqualToString:_contentPassword.text]){
        [self tostView:@"两次新密码输入不一致，请重新输入"];
        return;
    }
    [self showHudText:@""];
    [HQNetWork requetParam:[HQParam userSpass:_phoneNo.text withNum:_oldPassword.text withPass:_newPassword.text] withMethod:[HQMethod userSpass] suc:^(NSDictionary *sucData) {
        [self hiddnHud];
        if([sucData[kStatus] integerValue] == 1){
            [self tostView:@"密码修改成功"];
            [self dismissViewControllerAnimated:YES completion:^{}];
        }else{
            [self tostView:sucData[kInfo]];
        }
    } fail:^{
        [self hiddenErrorHud];
    }];
}

/**
 关闭界面
 
 @param sender 关闭按钮
 */
- (IBAction)closeItem:(id)sender {
    [self dismissViewControllerAnimated:YES completion:^{}];
}

-(void)scrollViewDidScroll:(UIScrollView *)scrollView{
    if(scrollView.contentOffset.y <-50){
        [self.view endEditing:YES];
    }
}

@end
